# encoding: utf-8

class ApplicationController < ActionController::Base
  protect_from_forgery
  
  helper_method :current_admin
  
  before_filter :login_required
  
  def login_required
    unless current_admin
      flash[:notice] = "你还没有登录系统，请登录..."
      store_location
      redirect_to login_path
    end
  end
  
  protected

  def current_admin
    if session[:admin_user_id]
      @current_admin ||= AdminUser.find_by_id(session[:admin_user_id])
    end
  end
  
  def store_location
    session[:return_to] = request.path
  end
  
  def redirect_back_or_default(default = '/')
    redirect_to session[:return_to] ||= default
    session[:return_to] = nil
  end
  
  def clear_login_session
    session[:admin_user_id] = nil
  end
end
